﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ASpect.Logic;

namespace ASpect
{
    public partial class ASpect : Form
    {
        public ASpect()
        {
            InitializeComponent();
        }

        Audio file = new Audio(-1,44100);
        Graphic spectrum;
        int[] monfreq = new int[6] { 10, 15, 20, 25, 50, 60 };
        int ifreq = 1;
        BufferedGraphicsContext context;
        BufferedGraphics grafx;
        int x0, x1, y0, y1;
        System.Windows.Forms.Form faspect;
        EQForm feq;
        int lblnamemax = 16;
        int lbli=0;

        private void btn_pause_Click(object sender, EventArgs e)
        {
            btn_play.Show();
            btn_pause.Hide();
            btn_stop.Hide();
            btn_monfreq.Hide();
            ZoomSlider.Hide();
            DrawGraphTimer.Stop();
            file.Pause();
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            btn_play.Show();
            btn_pause.Hide();
            btn_stop.Hide();
            btn_monfreq.Hide();
            ZoomSlider.Hide();
            DrawGraphTimer.Stop();
            ScrollLabelTextTimer.Stop();
            btn_openfile.Show();
            lbl_MusicInfo.Hide();
            file.Stop();
        }

        private void btn_play_Click(object sender, EventArgs e)
        {
            btn_play.Hide();
            btn_openfile.Hide();
            btn_pause.Show();
            btn_stop.Show();
            btn_monfreq.Show();
            ZoomSlider.Show();
            ScrollLabelTextTimer.Start();
            lbl_MusicInfo.Show();
            file.Play();
            DrawGraphTimer.Start();
        }

        private void btn_openfile_Click(object sender, EventArgs e)
        {
            FBrowser.ShowDialog();
            try
            {
                if (!file.Start(ASpect.ActiveForm))
                    throw new Exception("Cannot find an output device");
                if (!file.LoadFile(FBrowser.FileName))
                    throw new Exception("Corrupted file");
                spectrum.ZoomDefault();
                feq.CreateEQ(file.Stream);
                btn_play.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
            }
        }

        private void ASpect_Load(object sender, EventArgs e)
        {
            Un4seen.Bass.BassNet.Registration("frantic1993@gmail.com", "2X16371021152222");
            btn_monfreq.Text = monfreq[ifreq].ToString() + " Hz";
            faspect = this;
            x0 = 20;
            x1 = faspect.Width - 70;
            y0 = 20;
            y1 = faspect.Height - 130;
            DrawGraphTimer.Interval = 1000 / monfreq[ifreq];
            context = BufferedGraphicsManager.Current;
            context.MaximumBuffer = new Size((int)((x1 - x0) * 1.1), (int)((y1-y0)*1.1));
            grafx = context.Allocate(faspect.CreateGraphics(),
                 new Rectangle((int)(x0*0.9), y0, (int)((x1-x0)*1.1), (int)((y1-y0)*1.1)));
            spectrum = new Graphic(x0, x1, y0, y1, grafx.Graphics);
            spectrum.Clear();
            feq = new EQForm();
        }

        private void DrawGraphTimer_Tick(object sender, EventArgs e)
        {
            if (file.PlaybackStat())
            {
                spectrum.Clear();
                spectrum.DrawGraphic(file.FFTData());
                faspect.Refresh();
            }
            else
            {
                btn_play.Show();
                btn_pause.Hide();
                btn_stop.Hide();
                btn_monfreq.Hide();
                ZoomSlider.Hide();
                DrawGraphTimer.Stop();
                ScrollLabelTextTimer.Stop();
                btn_openfile.Show();
                lbl_MusicInfo.Hide();
            }
        }

        private void ASpect_Paint(object sender, PaintEventArgs e)
        {
            spectrum.DrawAxes();
            grafx.Render();
        }

        private void btn_monfreq_Click(object sender, EventArgs e)
        {
            if (ifreq > 4)
                ifreq = -1;
            DrawGraphTimer.Interval =1000 / monfreq[++ifreq];
            btn_monfreq.Text = monfreq[ifreq].ToString() + " Hz";
        }

        private void ASpect_Resize(object sender, EventArgs e)
        {
            if (!file.PlaybackStat())
            {
                spectrum.Clear();
                faspect.Refresh();
            }
            x0 = 20;
            x1 = faspect.Width - 70;
            y0 = 20;
            y1 = faspect.Height - 130;
            context.MaximumBuffer = new Size((int)((x1 - x0) * 1.1), (int)((y1 - y0) * 1.1));
            grafx = context.Allocate(faspect.CreateGraphics(),
                 new Rectangle((int)(x0 * 0.9), y0, (int)((x1 - x0) * 1.1), (int)((y1 - y0) * 1.1)));
            spectrum.Resize(x0, x1, y0, y1, grafx.Graphics);
            spectrum.Clear();
        }

        private void ZoomSlider_Scroll(object sender, EventArgs e)
        {
            spectrum.Zoom(ZoomSlider.Value);
        }

        private void ScrollLabelTextTimer_Tick(object sender, EventArgs e)
        {
            lbl_MusicInfo.Text = file.MusicInfo(FBrowser.SafeFileName,lblnamemax,lbli++);
            if (lbli >= (lblnamemax-1))
                lbli = 0;
        }

        private void btn_eq_Click(object sender, EventArgs e)
        {
            if (feq.Visible == false)
                feq.Show();
            else
            {
                feq.Hide();
                feq.EQFlat();
            }
        }

    }
}
